/**
 * @file     addDigits.js
 * @brief    [258. 各位相加](https://leetcode.cn/problems/add-digits/)
 * @author   Zhu
 * @date     2023-02-12 13:28
 */

/**
 * @param {number} num
 * @return {number}
 */
var addDigits = function (num) {
    function sumDigits(num) {
        let level = 10;
        let sum = 0;
        do {
            let n = num % 10;
            sum += n;
            level *= 10;
            num = (num - n) / 10;
        } while (num > 0);

        return sum;
    }

    while (num > 9) {
        num = sumDigits(num);
    }
    return num;
};
